package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Druginfo;
import com.li.drug.service.DruginfoService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 药品相关controller
 */
@Controller
@RequestMapping("/druginfo")
public class DruginfoController {

    @Autowired
    DruginfoService druginfoService;

    /**
     * 转向药品页面
     * @return
     */
    @RequestMapping
    public String druginfo(){
        return "druginfo/druginfo";
    }

    //分页查询药品列表
    @RequestMapping("/druginfoQueryPage")
    @ResponseBody
    public Object druginfoQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Druginfo> page = druginfoService.selectDruginfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向药品新增页面
     * @return
     */
    @RequestMapping("/druginfoPage")
    public String druginfoPage(){
        return "druginfo/druginfoPage";
    }

    /**
     * 添加一个药品
     */
    @RequestMapping("/druginfoAdd")
    @ResponseBody
    public Object druginfo(Druginfo druginfo){
        int i = druginfoService.addDruginfo(druginfo);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向药品编辑页面
     * @return
     */
    @RequestMapping("/druginfoQueryById")
    public String druginfoEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Druginfo druginfo = druginfoService.getDruginfoById(id);
        model.addAttribute("druginfo",druginfo);
        return "druginfo/druginfoPage";
    }

    /**
     * 修改一个药品
     */
    @RequestMapping("/druginfoEdit")
    @ResponseBody
    public Object druginfoEdit(Druginfo druginfo){
        try {
            int i = druginfoService.editDruginfo(druginfo);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个药品
     */
    @RequestMapping("/druginfoDelById")
    @ResponseBody
    public Object druginfoDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = druginfoService.delDruginfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有药品列表
     */
    @RequestMapping("/druginfoList")
    @ResponseBody
    public Object druginfoList(){
        List<Druginfo> list = druginfoService.list();
        return list;
    }

    /**
     * 跳转到药品保质期检查页面
     */
    @RequestMapping("/warrenty")
    public String warrenty(){
        return "druginfo/warrenty";
    }
}
